<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
      size="small"
    >
      <el-form-item label="类型" prop="menuType">
        <el-radio-group v-model="dataForm.menuType" :disabled="!!dataForm.id">
          <el-radio
            v-for="(type, index) in typeList"
            :label="index"
            :key="index"
            >{{ type }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
      <el-form-item
        :label="typeList[dataForm.menuType] + '名称'"
        prop="menuName"
      >
        <el-input
          v-model="dataForm.menuName"
          :placeholder="typeList[dataForm.menuType] + '名称'"
        ></el-input>
      </el-form-item>
      <el-form-item
        v-if="dataForm.menuType !== 0"
        label="上级菜单"
        prop="parentName"
      >
        <el-popover
          ref="menuListPopover"
          placement="bottom-start"
          trigger="click"
          v-model="showTree"
        >
          <el-tree
            :data="menuList"
            :props="menuListTreeProps"
            node-key="menuId"
            ref="menuListTree"
            @current-change="menuListTreeCurrentChangeHandle"
            :default-expand-all="false"
            :highlight-current="true"
            :expand-on-click-node="false"
          >
          </el-tree>
        </el-popover>
        <el-input
          v-model="dataForm.parentName"
          v-popover:menuListPopover
          :readonly="true"
          placeholder="点击选择上级菜单"
          class="menu-list__input"
        ></el-input>
      </el-form-item>
      <el-form-item v-if="dataForm.menuType === 1" label="菜单路由" prop="url">
        <el-input v-model="dataForm.url" placeholder="菜单路由"></el-input>
      </el-form-item>
      <el-form-item
        v-if="dataForm.menuType !== 0"
        label="授权标识"
        prop="perms"
      >
        <el-input
          v-model="dataForm.perms"
          placeholder="多个用逗号分隔, 如: user:list,user:create"
        ></el-input>
      </el-form-item>
      <el-form-item
        v-if="dataForm.menuType !== 2"
        label="排序号"
        prop="orderNum"
      >
        <el-input-number
          v-model="dataForm.orderNum"
          controls-position="right"
          :min="0"
          label="排序号"
        ></el-input-number>
      </el-form-item>
      <el-form-item v-if="dataForm.menuType !== 2" label="菜单图标" prop="icon">
        <el-row>
          <el-col :span="22">
            <el-popover
              ref="iconListPopover"
              placement="bottom-start"
              trigger="click"
              popper-class="mod-menu__icon-popover"
            >
              <div class="mod-menu__icon-inner">
                <div class="mod-menu__icon-list">
                  <el-button
                    v-for="(item, index) in iconList"
                    :key="index"
                    @click="iconActiveHandle(item)"
                    :class="{ 'is-active': item === dataForm.icon }"
                  >
                    <icon-svg :name="item"></icon-svg>
                  </el-button>
                </div>
              </div>
            </el-popover>
            <el-input
              v-model="dataForm.icon"
              v-popover:iconListPopover
              :readonly="true"
              placeholder="菜单图标名称"
              class="icon-list__input"
            ></el-input>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false" size="small">取消</el-button>
      <el-button
        type="primary"
        size="small"
        @click="dataFormSubmit()"
        v-prevent-re-click
        >确定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
// import { treeDataTranslate } from '@/utils'
import Icon from "@/icons";
export default {
  data() {
    var validateUrl = (rule, value, callback) => {
      if (this.dataForm.menuType === 1 && !/\S/.test(value)) {
        callback(new Error("菜单URL不能为空"));
      } else {
        callback();
      }
    };
    return {
      visible: false,
      showTree: false,
      typeList: ["目录", "菜单", "按钮"],
      dataForm: {
        menuType: 1,
        menuName: "",
        parentId: 0,
        parentName: "",
        url: "",
        perms: "",
        orderNum: 0,
        icon: ""
      },
      dataRule: {
        menuName: [
          { required: true, message: "菜单名称不能为空", trigger: "blur" }
        ],
        parentName: [
          { required: true, message: "上级菜单不能为空", trigger: "change" }
        ],
        url: [{ validator: validateUrl, trigger: "blur" }]
      },
      menuList: [],
      menuListTreeProps: {
        label: "menuName",
        children: "list"
      }
    };
  },
  created() {
    this.iconList = Icon.getNameList();
  },
  methods: {
    async init(id) {
      this.dataForm.id = id || undefined;
      let [err, res] = await this.$Api.Menus.listMenu();
      if (!err) {
        this.menuList = res;
      }
      this.visible = true;
      await this.$nextTick();
      this.$refs["dataForm"].resetFields();
      if (id) {
        [err, res] = await this.$Api.Menus.detail({
          params: {
            id
          }
        });
        this.dataForm = res;
      }
    },
    // 菜单树选中
    menuListTreeCurrentChangeHandle(data) {
      this.dataForm.parentId = data.id;
      this.dataForm.parentName = data.menuName;
      this.showTree = false;
    },
    // 菜单树设置当前选中节点
    menuListTreeSetCurrentNode() {
      this.$refs.menuListTree.setCurrentKey(this.dataForm.parentId);
      this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() ||
        {})["name"];
    },
    // 图标选中
    iconActiveHandle(iconName) {
      this.dataForm.icon = iconName;
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(async valid => {
        if (valid) {
          let err;
          let data = { ...this.dataForm };
          if (this.dataForm.id) {
            [err] = await this.$Api.Menus.update({
              data
            });
          } else {
            [err] = await this.$Api.Menus.add({
              data
            });
          }
          if (!err) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {}
            });
            this.visible = false;
            this.$emit("refreshDataList");
          }
        }
      });
    }
  }
};
</script>

<style lang="scss">
.mod-menu {
  .menu-list__input,
  .icon-list__input {
    > .el-input__inner {
      cursor: pointer;
    }
  }
  &__icon-popover {
    width: 458px;
    overflow: hidden;
  }
  &__icon-inner {
    width: 478px;
    max-height: 258px;
    overflow-x: hidden;
    overflow-y: auto;
  }
  &__icon-list {
    width: 458px;
    padding: 0;
    margin: -8px 0 0 -8px;
    > .el-button {
      padding: 8px;
      margin: 8px 0 0 8px;
      > span {
        display: inline-block;
        vertical-align: middle;
        width: 18px;
        height: 18px;
        font-size: 18px;
      }
    }
  }
  .icon-list__tips {
    font-size: 18px;
    text-align: center;
    color: #e6a23c;
    cursor: pointer;
  }
}
</style>
